//
//  ByApiAddress.m
//  oc_knowledge
//
//  Created by mc on 17/1/12.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByApiAddress.h"

@implementation ByApiAddress
#pragma mark - 查询接口，返回data为数组
/** 地址查询 */
/** result.data 为数组 数组中包含 */
+ (void)addressQuery:(ByModelAddress *)model
             success:(void(^)(ByNetBaseResult *result))success
             failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"By_Address_query";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.uid forKey:@"uid"];
    [params setObject:model.s_id forKey:@"s_id"];
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeArray
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           // 返回数据 为数组
           NSAssert([responseObject isKindOfClass:[NSArray class]], @"地址查询 返回数据 必须是数组类型的，请检查");
           
           ALog(@"******** 地址查询 -> 成功: %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"地址查询成功";
           // 返回数组
           NSMutableArray *resAry = [[NSMutableArray alloc] init];
           for (NSDictionary *dic in (NSArray *)responseObject) {
               ByModelAddress *model = [[ByModelAddress alloc] initContentWithDic:dic];
               [resAry addObject:model];
           }
           result.data = resAry;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           ALog(@"******** 地址查询 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
    
}

#pragma mark - 获取接口
+ (void)addressGetInfo:(ByModelAddress *)model
               success:(void(^)(ByNetBaseResult *result))success
               failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"BY_Address_getInfo";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.uid forKey:@"uid"];
    [params setObject:model.address_id forKey:@"id"];
    [params setObject:model.s_id forKey:@"s_id"];
    
    // 返回的responseObject 可能是字符串也可能是字典
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeDictionary
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           // 返回数据 为字典
           NSAssert([responseObject isKindOfClass:[NSDictionary class]], @"获取单个地址 返回数据 必须是字典类型的，请检查");
           
           ALog(@"******** 单个地址获取 -> 成功: %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"单个地址获取成功";
           // 返回字典
           ByModelAddress *model = [[ByModelAddress alloc] initContentWithDic:responseObject];
           result.data = model;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           ALog(@"******** 单个地址获取 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
}

/** 获取默认地址 */
/** result.data 为字符串 默认的地址id */
+ (void)defaultAddressGet:(ByModelAddress *)model
                  success:(void(^)(ByNetBaseResult *result))success
                  failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"BY_Address_getDefault";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.uid forKey:@"uid"];
    [params setObject:model.s_id forKey:@"s_id"];
    
    // 返回的responseObject 可能是字符串也可能是字典
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeDictionary
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           // 返回数据 为字符串
           NSAssert([responseObject isKindOfClass:[NSDictionary class]], @"获取默认地址id 返回数据 必须是字典类型的，请检查");
           
           ALog(@"******** 默认地址获取 -> 成功: %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"默认地址id获取成功";
           // 返回字典
           ByModelAddress *model = [[ByModelAddress alloc] initContentWithDic:responseObject];
           result.data = model;
           
           
           if(success) {
               success(result);
           }
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           ALog(@"******** 默认地址获取 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
}

#pragma mark - 上传接口，返回data为字符串
/** 地址添加 */
+ (void)addressAdd:(ByModelAddress *)model
           success:(void(^)(ByNetBaseResult *result))success
           failure:(void(^)(ByNetBaseResult *result))failure {
    
    if (model.contactname.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请填写收货人的姓名"];
        return;
    }
    if (model.mobile.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请填写手机号"];
        return;
    }
    if (model.mobile.length != 11) {
        [ByCoreToolAlert showOnlyText:@"请输入正确手机号"];
        return;
    }
    if (model.province.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请选择所在地区"];
        return;
    }
    if (model.detailinfo.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请填写详细地址"];
        return;
    }
    
    static NSString *apiType = @"By_Address_add";
    static NSInteger apiVersion = 102;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.uid forKey:@"uid"];
    [params setObject:@"中国" forKey:@"country"];
    [params setObject:model.s_id forKey:@"s_id"];
    
    if (model.provinceid) {
        [params setObject:model.provinceid forKey:@"provinceid"];
        
    }
    if (model.province) {
        [params setObject:model.province forKey:@"province"];
        
    }
    
    if (model.cityid) {
        [params setObject:model.cityid forKey:@"cityid"];
        
    }
    if (model.city) {
        [params setObject:model.city forKey:@"city"];
        
    }
    if (model.areaid) {
        [params setObject:model.areaid forKey:@"areaid"];
        
    }
    if (model.area) {
        [params setObject:model.area forKey:@"area"];
        
    }
    [params setObject:model.detailinfo forKey:@"detailinfo"];
    [params setObject:model.contactname forKey:@"contactname"];
    [params setObject:model.mobile forKey:@"mobile"];
    [params setObject:@"111" forKey:@"postal_code"];
    [params setObject:model.is_default forKey:@"default"];
    [params setObject:@"1" forKey:@"country_id"];
    // 返回的responseObject 可能是字符串也可能是字典
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           // 返回数据 为字符串
           NSAssert([responseObject isKindOfClass:[NSString class]], @"地址添加 返回数据 必须是字符串类型的，请检查");
           
           ALog(@"******** 地址添加 -> 成功: %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"地址添加成功";
           result.data = responseObject; // 地址id
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           ALog(@"******** 地址添加 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
}

/** 地址更新 */
+ (void)addressUpdate:(ByModelAddress *)model
              success:(void(^)(ByNetBaseResult *result))success
              failure:(void(^)(ByNetBaseResult *result))failure {
    if (model.contactname.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请填写收货人的姓名"];
        return;
    }
    if (model.mobile.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请填写手机号"];
        return;
    }
    if (model.mobile.length != 11) {
        [ByCoreToolAlert showOnlyText:@"请输入正确手机号"];
        return;
    }
    if (model.province.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请选择所在地区"];
        return;
    }
    if (model.detailinfo.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请填写详细地址"];
        return;
    }

    static NSString *apiType = @"BY_Address_update";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.address_id forKey:@"id"];
    [params setObject:model.uid forKey:@"uid"];
    [params setObject:model.is_default forKey:@"default"];
    [params setObject:@"中国" forKey:@"country"];
    [params setObject:@"1" forKey:@"country_id"];
    [params setObject:@"1111" forKey:@"postal_code"];
    [params setObject:model.s_id forKey:@"s_id"];
    
    if (model.provinceid) {
        [params setObject:model.provinceid forKey:@"provinceid"];
    }
    if (model.cityid) {
        [params setObject:model.cityid forKey:@"cityid"];
    }
    if (model.areaid) {
        [params setObject:model.areaid forKey:@"areaid"];
    }
    if (model.detailinfo) {
        [params setObject:model.detailinfo forKey:@"detailinfo"];
    }
    if (model.contactname) {
        [params setObject:model.contactname forKey:@"contactname"];
    }
    if (model.mobile) {
        [params setObject:model.mobile forKey:@"mobile"];
    }
    if (model.postal_code) {
        [params setObject:model.postal_code forKey:@"postal_code"];
    }
    if (model.province) {
        [params setObject:model.province forKey:@"province"];
    }
    if (model.city) {
        [params setObject:model.city forKey:@"city"];
    }
    if (model.area) {
        [params setObject:model.area forKey:@"area"];
    }
    if (model.id_card) {
        [params setObject:model.id_card forKey:@"id_card"];
    }
    if (model.is_default) {
        [params setObject:model.is_default forKey:@"isDefault"];
    }
    
    // 返回的responseObject 可能是字符串也可能是字典
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           // 返回数据 为字符串
           NSAssert([responseObject isKindOfClass:[NSString class]], @"地址更新 返回数据 必须是字符串类型的，请检查");
           
           ALog(@"******** 地址更新 -> 成功: %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"地址更新成功";
           result.data = responseObject; // 地址id
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           ALog(@"******** 地址更新 -> 失败: %@", error);
           
           
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
}
/** 地址删除 */
+ (void)addressDelete:(ByModelAddress *)model
              success:(void(^)(ByNetBaseResult *result))success
              failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"BY_Address_delete";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.address_id forKey:@"id"];
    [params setObject:model.uid forKey:@"uid"];
    [params setObject:model.s_id forKey:@"s_id"];
    
    // 返回的responseObject 可能是字符串也可能是字典
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           // 返回数据 为字符串
           NSAssert([responseObject isKindOfClass:[NSString class]], @"地址删除 返回数据 必须是字符串类型的，请检查");
           
           ALog(@"******** 地址删除 -> 成功: %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"地址删除成功";
           result.data = responseObject;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           ALog(@"******** 地址删除 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
}
/** 设置默认地址 */
+ (void)defaultAddressSet:(ByModelAddress *)model
                  success:(void(^)(ByNetBaseResult *result))success
                  failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"BY_Address_setDefault";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.address_id forKey:@"id"];
    [params setObject:model.s_id forKey:@"s_id"];
    
    // 返回的responseObject 可能是字符串也可能是字典
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           // 返回数据 为字符串
           NSAssert([responseObject isKindOfClass:[NSString class]], @"设置默认地址 返回数据 必须是字符串类型的，请检查");
           
           ALog(@"******** 设置默认地址 -> 成功: %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"设置默认地址成功";
           result.data = responseObject;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           ALog(@"******** 设置默认地址 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
}

#pragma mark ---暂时用来 测试使用
/** 登陆 */
+ (void)loginTestSuccess:(void(^)(ByNetBaseResult *result))success
                 failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"By_User_login";
    static NSInteger apiVersion = 104;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%.0lf%d", [NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:@"18267152148" forKey:@"username"];
    [params setObject:@"123456" forKey:@"password"];
    [params setObject:@"role_driver" forKey:@"role"];
    [params setObject:@"+86" forKey:@"country"];
    // 返回的responseObject 可能是字符串也可能是字典
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeDictionary
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"登录成功";
           result.data = responseObject;
           result.notify_id = notifyID;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
//           UserLog(@"******** 登录 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
    
}


@end
